package interview;

public class interview_05_08 {
    public int[] drawLine(int length, int w, int x1, int x2, int y) {
        int number=w/32;
        int index=(y)*number;
        int[] result=new int[length];
        int start=index+x1/32;
        int end=index+x2/32;
        for(int i=start+1;i<end;++i){
            result[i]=-1;
        }
        if(start==end){
            int index1=x1%32;
            int index2=x2%32;
            int change=0;
            for(int i=0;i<=index2-index1;++i){
                change=change<<1;
                change=change|1;
            }
            for(int i=index2;i<31;++i){
                change=change<<1;
            }
            result[start]=result[start]|change;
            return result;
        }
        int left=31-(x1%32)+1;
        int tmp=0;
        for(int i=0;i<left;++i){
            tmp=tmp<<1;
            tmp=tmp|1;
        }
        result[start]=result[start]|tmp;
        left=x2%32;
        tmp=0;
        for(int i=0;i<=left;++i){
            tmp=tmp<<1;
            tmp=tmp|1;
        }
        tmp=tmp<<(31-left);
        result[end]=result[end]|tmp;
        return result;
    }
}
